package Passageiros;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Vo.StatusReservasVO;

public class StatusReservasDAO extends DataSourceDAO
{
	List<StatusReservasVO> comboStatusreserva = new ArrayList<StatusReservasVO>();
	public StatusReservasDAO( ) 
	{
		
	}
	
	//metodo responsavel por retornar todas os Status das reservas disponiveis no modelo fisico de dados
	public List<StatusReservasVO> listarStatusReserva() throws DAOException
	{
		StatusReservasVO statusReservas = null;
        String select = "SELECT * FROM STATUSRESERVAS";
        PreparedStatement pstmt	= null;
        ResultSet rs = null;
        
		try
		{
			connect( ) ;
            pstmt = connection.prepareStatement( select );
            //pstmt.setInt( 1, codigo ) ; // Narcisio, verificar esta linha
            rs = pstmt.executeQuery( ) ;
            
            rs.beforeFirst();
                
            while (rs.last() != false)
            {
            	String descricaoStatusReserva = rs.getString("descricao");
            	statusReservas = new StatusReservasVO( );
            	statusReservas.setDescricaoStatusReservas(descricaoStatusReserva);
                    
            	statusReservas.setCodigoStatusReservas(rs.getInt("codigo"));
                    
            	comboStatusreserva.add(statusReservas);              
            }
         }
		 catch ( SQLException e ) 
		 {
			e.printStackTrace();
			throw new DAOException("Impossivel listar o status das reservas!", e );
		 } 
		 finally 
		 {
		     try 
			 {
				 if ( pstmt != null ) 
                 {
                    pstmt.close();
                 }
                 if ( rs != null ) 
                 {
                    rs.close();
                 }
                 close( ) ;
			 } 
		     catch ( SQLException e ) 
             {
		    	 e.printStackTrace( ) ;
             }
		 }
		 return comboStatusreserva;         
	}
	//#########################################################################################

    public StatusReservasVO consultarStatusReserva( int codigo ) throws DAOException  
    {
    	StatusReservasVO statusReservas = null;
        String select = "SELECT * FROM STATUSRESERVAS WHERE CODIGO = ?";
        PreparedStatement pstmt	= null;
        ResultSet rs = null;
        
        try 
        {
        	connect( ) ;
            pstmt = connection.prepareStatement( select );
            pstmt.setInt( 1, codigo ) ;
            rs = pstmt.executeQuery( ) ;
            if ( rs.next() ) 
            {
                String descricao = rs.getString("descricao");
                
                statusReservas = new StatusReservasVO( );
                statusReservas.setCodigoStatusReservas( codigo );
                statusReservas.setDescricaoStatusReservas(descricao);               
            }
        } 
        catch ( SQLException e ) 
        {
            e.printStackTrace();
            throw new DAOException("Impossivel consultar statusReservas!", e );
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                if ( rs != null ) 
                {
                    rs.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( ) ;
            }
        }
        return statusReservas ;
    }
    
	public void inserirStatusReserva( StatusReservasVO statusReservas ) throws DAOException  
    {
        String insert = "INSERT INTO STATUSRESERVAS ( CODIGO, DESCRICAO) VALUES (?,?)";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement(insert);
            pstmt.setInt(1, statusReservas.getCodigoStatusReservas());
            pstmt.setString(2, statusReservas.getDescricaoStatusReservas());
           
            pstmt.executeUpdate( ) ;
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel inserir statusReservas!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( );
            }
        }
    }
    
    public void excluirStatusReserva( StatusReservasVO statusReservas )  throws DAOException  
    {
        String delete = "DELETE FROM STATUSRESERVAS WHERE CODIGO = ?";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement( delete );
            pstmt.setInt( 1,statusReservas.getCodigoStatusReservas( ) ) ;
            pstmt.executeUpdate( ) ;
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel deletar statusReservas!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( ) ;
            }
        }
    }
    
    public void alterarStatusReserva( StatusReservasVO statusReservas ) throws DAOException  
    {
        String insert = "UPDATE STATUSRESERVAS SET DESCRICAO = ? WHERE CODIGO = ?";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement(insert);
            pstmt.setString(1, statusReservas.getDescricaoStatusReservas());
            pstmt.setInt(2, statusReservas.getCodigoStatusReservas());
            pstmt.executeUpdate ();
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel alterar statusReservas!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace();
            }
        }
    }
}
